//
//  LSProgressHUD.m
//  lightSpot
//
//  Created by huangxuan on 2018/8/29.
//  Copyright © 2018年 huangxuan. All rights reserved.
//

#import "LSProgressHUD.h"

@interface LSProgressHUD ()
    
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgWidth;

@end

@implementation LSProgressHUD

+ (id)instanceView {
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
    return [nibView objectAtIndex:0];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Do any additional setup after loading the view.
}

- (void)setData:(id)data delegate:(id)delegate {
    if ([data isKindOfClass:[LSProgressHUDModel class]]) {
        LSProgressHUDModel *model = data;
        _contentLabel.text = model.title;
        
        CGSize contentSize = [_contentLabel.text ex_sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
        if (contentSize.width > kScreenWidth - 32 - 40) {
            contentSize = [_contentLabel.text ex_sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(kScreenWidth - 32 - 40, MAXFLOAT)];
            _bgWidth.constant = kScreenWidth - 32;
            _viewHeight.constant = 40 + contentSize.height - 16;
        } else {
            _bgWidth.constant = contentSize.width + 40 + 10;
            _viewHeight.constant = 40;
        }
        
        [self performSelector:@selector(delayMethod) withObject:nil afterDelay:2.0];
    }
}

- (void)delayMethod {
    [UIView animateWithDuration:0.5 animations:^{
        self.myView.alpha = 0;
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

@end

#pragma mark - Model

@implementation LSProgressHUDModel
    
+ (LSProgressHUDModel *)ittemModelWithTitle:(NSString *)title {
    LSProgressHUDModel *model = [LSProgressHUDModel new];
    model.title = kSafeString(title);
    return model;
}
    
@end
